﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Stocks;

namespace Aandelendelen.Obs
{
	public class PortfolioDetails : BaseDataTable
	{
		decimal totalBuy;
		decimal totalValue;
		decimal totalProfit;

		public PortfolioDetails(int userId)
			: base("Porteteuille")
		{
			this.csvHeader = "Fonds, Aantal, Aankoop, Laatse koers, Winst, Verkoopbereidheid, Verkopen, Invloed";

			this.Columns.Add(new DataColumn("Fonds", typeof(string)));
			this.Columns.Add(new DataColumn("Aantal", typeof(string)));
			this.Columns.Add(new DataColumn("AankoopDatum", typeof(string)));
			this.Columns.Add(new DataColumn("KoersToen", typeof(string)));
			this.Columns.Add(new DataColumn("Betaald", typeof(string)));
			this.Columns.Add(new DataColumn("Laatste", typeof(string)));
			this.Columns.Add(new DataColumn("KoersHuidig", typeof(string)));
			this.Columns.Add(new DataColumn("Waarde", typeof(string)));
			this.Columns.Add(new DataColumn("Winst", typeof(string)));
			this.Columns.Add(new DataColumn("Verkoopbereidheid", typeof(string)));
			this.Columns.Add(new DataColumn("Verkopen", typeof(string)));
			this.Columns.Add(new DataColumn("Invloed", typeof(string)));

			Query query = new Query("s_portfoliodetails", "NawId", userId, OleDbType.Integer);

			ResultSet resultSet = (ResultSet)query.Execute();

			this.AddDetails(resultSet);
		}

		public decimal TotalBuy { get { return this.totalBuy; } }
		public decimal TotalValue { get { return this.totalValue; } }
		public decimal TotalProfit { get { return this.totalProfit; } }

		private void AddDetails(ResultSet resultSet)
		{
			int numRows = resultSet.Count;

			totalBuy = 0;
			totalValue = 0;

			for (int i = 0; i < numRows; i++)
			{
				int fonds = (int)resultSet[i, "stockid"];
				Stock stock = StockCollection.GetInstance()[fonds];

				decimal number = (decimal)resultSet[i, "numberofstocks"];
				
				DateTime datePurchased = (DateTime)resultSet[i, "transdate"];
				string strDate = ObjectFormatter.GetFormattedDate(datePurchased);
				decimal purchasePrice = (decimal)resultSet[i, "price"];
				string aankoop = ObjectFormatter.GetRateString(purchasePrice);
				decimal buyCost = purchasePrice * number;
				totalBuy += buyCost;
				string total = ObjectFormatter.GetCurrencyString(buyCost);

				decimal currentPrice = stock.CurrentRate.Amount;
				string currentDate = ObjectFormatter.GetFormattedDateTimeOnly(stock.CurrentRate.Date);
				string laatste = ObjectFormatter.GetRateString(currentPrice);

				decimal currentValue = number * currentPrice;
				totalValue += currentValue;
				string waarde = ObjectFormatter.GetCurrencyString(currentValue);

				string winst = ObjectFormatter.GetProfitPercentageString(purchasePrice, currentPrice);

				string verkoopBereidheid = "TODO";

				string verkopen = ((bool)resultSet[i, "salerequested"]) ? "Ja" : "Nee";

				string invloed = "TODO";

				DataRow row = this.NewRow();
				row[0] = stock.Name;
				row[1] = ObjectFormatter.GetNumberOfStocks(number);
				row[2] = strDate;
				row[3] = aankoop;
				row[4] = total;
				row[5] = currentDate;
				row[6] = laatste;
				row[7] = waarde;
				row[8] = winst;
				row[9] = verkoopBereidheid;
				row[10] = verkopen;
				row[11] = invloed;

				this.Rows.Add(row);
			}

			totalProfit = Calculator.GetProfitPercentage(totalBuy, totalValue);
		}
	}
}